<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>技能共享平台</title>
    <!-- 引入Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script>
        // 配置API基础URL
        const API_BASE_URL = 'http://localhost:3002/api';
    </script>
    <script>
        // 配置Tailwind自定义颜色和字体
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#4F46E5', // 主色调：靛蓝色
                        secondary: '#10B981', // 辅助色：绿色
                        neutral: '#F3F4F6', // 中性色：浅灰
                        dark: '#1F2937', // 深色文本
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .card-shadow {
                box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
            }
            .card-hover {
                transition: all 0.3s ease;
            }
            .card-hover:hover {
                transform: translateY(-5px);
            }
        }
    </style>
</head>
<body class="bg-gray-50 font-sans text-dark">
    <!-- 导航栏 -->
    <nav class="bg-white shadow-md fixed w-full z-10 top-0 left-0">
        <div class="container mx-auto px-4 py-3 flex justify-between items-center">
            <div class="flex items-center space-x-2">
                <i class="fa fa-handshake-o text-primary text-2xl"></i>
                <h1 class="text-xl font-bold text-primary">技能共享平台</h1>
            </div>
            <button class="bg-primary hover:bg-primary/90 text-white px-4 py-2 rounded-lg shadow-md transition duration-300 flex items-center space-x-2">
                <i class="fa fa-plus"></i>
                <span>发布技能</span>
            </button>
        </div>
    </nav>

    <!-- 主内容区 -->
    <main class="container mx-auto px-4 pt-24 pb-16">
        <!-- 页面标题 -->
        <div class="mb-8 text-center">
            <h2 class="text-[clamp(1.75rem,4vw,2.5rem)] font-bold text-dark mb-2">发现社区技能</h2>
            <p class="text-gray-600 max-w-2xl mx-auto">连接社区中的技能提供者与需求者，共建互助共享的社区生态</p>
        </div>

        <!-- 筛选栏 -->
        <div class="bg-white rounded-xl shadow-sm p-4 mb-8 flex flex-col md:flex-row gap-4 justify-between items-stretch md:items-center">
            <div class="w-full md:w-auto flex-1">
                <label class="block text-sm font-medium text-gray-700 mb-1">选择类别</label>
                <div class="relative">
                    <select id="category-filter" class="block w-full pl-3 pr-10 py-2.5 text-base border border-gray-300 focus:outline-none focus:ring-2 focus:ring-primary focus:border-primary sm:text-sm rounded-lg appearance-none bg-white">
                        <option value="0">全部类别</option>
                        <option value="生活服务">生活服务</option>
                        <option value="教育培训">教育培训</option>
                        <option value="维修服务">维修服务</option>
                        <option value="咨询服务">咨询服务</option>
                        <option value="其他技能">其他技能</option>
                    </select>
                    <div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
                        <i class="fa fa-chevron-down text-xs"></i>
                    </div>
    
            </div>
            </div>
            <div class="w-full md:w-auto flex-1">
                <label class="block text-sm font-medium text-gray-700 mb-1">服务方式</label>
                <div class="relative">
                    <select id="service-method-filter" class="block w-full pl-3 pr-10 py-2.5 text-base border border-gray-300 focus:outline-none focus:ring-2 focus:ring-primary focus:border-primary sm:text-sm rounded-lg appearance-none bg-white">
                        <option value="0">全部方式</option>
                        <option value="online">线上服务</option>
                        <option value="offline">线下服务</option>
                        <option value="both">两者皆可</option>
                    </select>
                    <div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
                        <i class="fa fa-chevron-down text-xs"></i>
                    </div>
                </div>
            </div>
            <div class="w-full md:w-auto flex items-end">
                <button id="search-btn" class="w-full md:w-auto bg-primary hover:bg-primary/90 text-white px-6 py-2.5 rounded-lg shadow-md transition duration-300 flex items-center justify-center space-x-2">
                    <i class="fa fa-search"></i>
                    <span>搜索</span>
                </button>
            </div>
        </div>

        <!-- 测试按钮 -->
        <div class="bg-white rounded-xl shadow-sm p-4 mb-8 text-center">
            <button id="test-api-btn" class="bg-primary hover:bg-primary/90 text-white px-6 py-2.5 rounded-lg shadow-md transition duration-300 flex items-center justify-center space-x-2 mx-auto">
                <i class="fa fa-play-circle"></i>
                <span>测试API连接</span>
            </button>
        </div>

        <!-- 调试信息 -->
        <div id="debug-info" class="bg-white rounded-xl shadow-sm p-4 mb-8 hidden">
            <h3 class="text-lg font-bold text-primary mb-2">调试信息</h3>
            <pre id="debug-output" class="text-sm bg-gray-50 p-3 rounded-lg overflow-auto max-h-64"></pre>
        </div>

        <!-- 技能列表 -->
        <div id="skills-container" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
            <!-- 技能卡片将通过JavaScript动态加载 -->
            <div class="col-span-full text-center py-16">
                <i class="fa fa-circle-o-notch fa-spin text-primary text-4xl mb-4"></i>
                <p class="text-gray-600">加载中...</p>
            </div>
        </div>

        <!-- 分页 -->
        <div id="pagination" class="mt-8 flex justify-center hidden">
            <nav class="inline-flex rounded-md shadow-sm" aria-label="Pagination">
                <a href="#" class="relative inline-flex items-center px-4 py-2 rounded-l-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                    <i class="fa fa-chevron-left text-xs"></i>
                </a>
                <a href="#" class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-primary text-sm font-medium text-white">
                    1
                </a>
                <a href="#" class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50">
                    2
                </a>
                <span class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700">
                    ...
                </span>
                <a href="#" class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50">
                    5
                </a>
                <a href="#" class="relative inline-flex items-center px-4 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                    <i class="fa fa-chevron-right text-xs"></i>
                </a>
            </nav>
        </div>
    </main>

    <!-- 页脚 -->
    <footer class="bg-dark text-white py-8">
        <div class="container mx-auto px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <div class="flex items-center space-x-2">
                        <i class="fa fa-handshake-o text-primary text-xl"></i>
                        <h2 class="text-lg font-bold">技能共享平台</h2>
                    </div>
                    <p class="text-gray-400 text-sm mt-1">共建互助共享的社区生态</p>
                </div>
                <div class="flex space-x-4">
                    <a href="#" class="text-gray-400 hover:text-primary transition duration-300">
                        <i class="fa fa-weixin text-xl"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-primary transition duration-300">
                        <i class="fa fa-weibo text-xl"></i>
                    </a>
                    <a href="#" class="text-gray-400 hover:text-primary transition duration-300">
                        <i class="fa fa-qq text-xl"></i>
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-700 mt-6 pt-6 text-center text-gray-400 text-sm">
                <p>© 2025 技能共享平台. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <!-- JavaScript -->
    <script>
        // 获取DOM元素
        const skillsContainer = document.getElementById('skills-container');
        const categoryFilter = document.getElementById('category-filter');
        const serviceMethodFilter = document.getElementById('service-method-filter');
        const searchBtn = document.getElementById('search-btn');
        const pagination = document.getElementById('pagination');
        const debugInfo = document.getElementById('debug-info');
        const debugOutput = document.getElementById('debug-output');

        // 加载技能数据
        async function loadSkills() {
            showDebugInfo('开始加载技能数据...');
            showDebugInfo(`API_BASE_URL: ${API_BASE_URL}`);
            try {
                showDebugInfo('构建查询参数...');
                // 显示加载状态
                skillsContainer.innerHTML = `
                    <div class="col-span-full text-center py-16">
                        <i class="fa fa-circle-o-notch fa-spin text-primary text-4xl mb-4"></i>
                        <p class="text-gray-600">加载中...</p>
                        <p class="text-sm text-gray-500 mt-2">API: ${API_BASE_URL}/skills</p>
                    </div>
                `;

                // 构建查询参数
                const params = {
                    page: 1,
                    category: categoryFilter.value === '0' ? '' : categoryFilter.value,
                    serviceMethod: serviceMethodFilter.value === '0' ? '' : serviceMethodFilter.value
                };
                showDebugInfo(`查询参数: ${JSON.stringify(params)}`);

                // 构建查询字符串
                const queryString = new URLSearchParams(params).toString();
                showDebugInfo(`查询字符串: ${queryString}`);

                // 发送请求
                showDebugInfo(`发送请求到: ${API_BASE_URL}/skills?${queryString}`);
                const response = await fetch(`${API_BASE_URL}/skills?${queryString}`);
                showDebugInfo(`收到响应，状态码: ${response.status}`);
                 
                if (!response.ok) {
                    throw new Error(`HTTP错误! 状态码: ${response.status}`);
                }
                 
                const data = await response.json();
                showDebugInfo(`响应数据: ${JSON.stringify(data)}`);

                // 检查响应
                if (!data.success || !data.data || !data.data.data) {
                    throw new Error('获取技能数据失败: 响应格式不正确');
                }

                const skills = data.data.data;

                // 显示技能数据
                renderSkills(skills);

                // 显示分页（如果有多个页面）
                if (data.data.total > 10) {
                    pagination.classList.remove('hidden');
                } else {
                    pagination.classList.add('hidden');
                }
            } catch (error) {
                console.error('加载技能失败:', error);
                skillsContainer.innerHTML = `
                    <div class="col-span-full text-center py-16">
                        <i class="fa fa-exclamation-circle text-red-500 text-4xl mb-4"></i>
                        <p class="text-gray-600">加载失败: ${error.message}</p>
                        <button onclick="loadSkills()" class="mt-4 bg-primary hover:bg-primary/90 text-white px-4 py-2 rounded-lg shadow-md transition duration-300">
                            重试
                        </button>
                    </div>
                `;
            }
        }

        // 渲染技能列表
        function renderSkills(skills) {
            if (skills.length === 0) {
                skillsContainer.innerHTML = `
                    <div class="col-span-full text-center py-16">
                        <i class="fa fa-frown-o text-gray-400 text-4xl mb-4"></i>
                        <p class="text-gray-600">暂无技能数据</p>
                        <button class="mt-4 bg-primary hover:bg-primary/90 text-white px-4 py-2 rounded-lg shadow-md transition duration-300">
                            发布第一个技能
                        </button>
                    </div>
                `;
                return;
            }

            let html = '';
            skills.forEach(skill => {
                // 服务方式文本
                let serviceMethodText = '未知';
                switch (skill.serviceMethod) {
                    case 'online':
                        serviceMethodText = '线上服务';
                        break;
                    case 'offline':
                        serviceMethodText = '线下服务';
                        break;
                    case 'both':
                        serviceMethodText = '两者皆可';
                        break;
                }

                // 生成技能卡片HTML
                html += `
                    <div class="bg-white rounded-xl overflow-hidden shadow-md card-shadow card-hover">
                        <div class="p-6">
                            <div class="flex justify-between items-start mb-4">
                                <div>
                                    <h3 class="text-lg font-bold text-dark mb-1">${skill.name || '未知技能'}</h3>
                                    <span class="inline-block px-3 py-1 bg-primary/10 text-primary text-xs font-medium rounded-full">
                                        ${skill.category || '未分类'}
                                    </span>
                                </div>
                                <span class="px-3 py-1 bg-green-100 text-green-800 text-xs font-medium rounded-full">
                                    已认证
                                </span>
                            </div>
                            <p class="text-gray-600 mb-4 line-clamp-2">
                                ${skill.description || '暂无描述'}
                            </p>
                            <div class="space-y-2 mb-6">
                                <div class="flex items-center text-sm text-gray-600">
                                    <i class="fa fa-map-marker w-5 text-primary"></i>
                                    <span class="ml-2">${skill.location || '社区内'}</span>
                                </div>
                                <div class="flex items-center text-sm text-gray-600">
                                    <i class="fa fa-clock-o w-5 text-primary"></i>
                                    <span class="ml-2">${skill.serviceTime || '灵活安排'}</span>
                                </div>
                                <div class="flex items-center text-sm text-gray-600">
                                    <i class="fa fa-wrench w-5 text-primary"></i>
                                    <span class="ml-2">${serviceMethodText}</span>
                                </div>
                                <div class="flex items-center text-sm text-gray-600">
                                    <i class="fa fa-calendar w-5 text-primary"></i>
                                    <span class="ml-2">发布时间: ${skill.createTime || '未知'}</span>
                                </div>
                            </div>
                            <div class="flex space-x-3">
                                <button class="flex-1 bg-primary hover:bg-primary/90 text-white px-4 py-2 rounded-lg shadow-sm transition duration-300 flex items-center justify-center space-x-2">
                                    <i class="fa fa-phone"></i>
                                    <span>联系</span>
                                </button>
                                <button class="flex-1 bg-white border border-gray-300 hover:bg-gray-50 text-gray-700 px-4 py-2 rounded-lg shadow-sm transition duration-300 flex items-center justify-center space-x-2">
                                    <i class="fa fa-info-circle"></i>
                                    <span>详情</span>
                                </button>
                            </div>
                        </div>
                    </div>
                `;
            });

            skillsContainer.innerHTML = html;
        }

        // 显示调试信息
        function showDebugInfo(message) {
            console.log('[API测试]', message);
            // 使用已有的调试信息容器
            debugInfo.classList.remove('hidden');
            debugOutput.textContent += `[${new Date().toLocaleTimeString()}] ${message}\n`;
            debugOutput.scrollTop = debugOutput.scrollHeight;
        }

        // 测试API连接
        const testApiBtn = document.getElementById('test-api-btn');
        testApiBtn.addEventListener('click', async function() {
            showDebugInfo('开始测试API连接...');
            showDebugInfo(`API_BASE_URL: ${API_BASE_URL}`);
            
            try {
                const testUrl = `${API_BASE_URL}/skills`;
                showDebugInfo(`发送测试请求到: ${testUrl}`);
                
                const response = await fetch(testUrl);
                showDebugInfo(`收到响应，状态码: ${response.status}`);
                
                if (!response.ok) {
                    throw new Error(`HTTP错误! 状态码: ${response.status}`);
                }
                
                const data = await response.json();
                showDebugInfo(`响应数据: ${JSON.stringify(data)}`);
                
                if (data.success && data.data && data.data.data) {
                    showDebugInfo(`测试成功! 获取到 ${data.data.data.length} 条技能数据`);
                    alert('API测试成功! 已获取到技能数据');
                } else {
                    showDebugInfo('测试失败: 响应格式不正确');
                    alert('API测试失败: 响应格式不正确');
                }
            } catch (error) {
                showDebugInfo(`测试失败: ${error.message}`);
                alert(`API测试失败: ${error.message}`);
            }
        });

        // 事件监听
        searchBtn.addEventListener('click', loadSkills);
        categoryFilter.addEventListener('change', loadSkills);
        serviceMethodFilter.addEventListener('change', loadSkills);

        // 初始加载
        window.addEventListener('DOMContentLoaded', loadSkills);
    </script>
</body>
</html>